<template>
  <div data-tauri-drag-region class="action-bar">
    <!-- 最小化 -->
    <div @click="appWindow.minimize()" class="hover-box">
      <svg class="mize">
        <use href="#maximize"></use>
      </svg>
    </div>
    <!-- 最大化 -->
    <div @click="restoreWindow" class="hover-box">
      <svg v-show="!windowMaximized" class="rectangle-small">
        <use href="#rectangle-small"></use>
      </svg>
      <svg v-show="windowMaximized" class="internal-reduction ">
        <use href="#internal-reduction"></use>
      </svg>
    </div>
    <!-- 关闭窗口 -->
    <div @click="handleCloseWin" :class="{ windowMaximized: 'rounded-rt-8px' }" class="action-close">
      <svg class="close">
        <use href="#close"></use>
      </svg>
    </div>
  </div>
</template>

<script setup lang="ts">
import { WebviewWindow } from '@tauri-apps/api/webviewWindow'
import { onMounted, onUnmounted, ref } from 'vue'

const appWindow = WebviewWindow.getCurrent();

// 窗口是否最大化状态
const windowMaximized = ref(false)


/** 恢复窗口大小 */
const restoreWindow = async () => {
  if (windowMaximized.value) {
    await appWindow.unmaximize()
  } else {
    await appWindow.maximize()
  }
}

// 判断当前是否是最大化
const handleResize = () => {
  appWindow.isMaximized().then((res) => {
    windowMaximized.value = res
  })
}

/** 处理关闭窗口事件 */
const handleCloseWin = async () => {
  await appWindow.close()
}

onMounted(async () => {
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.action-bar {
  position: absolute;
  top: 0;
  right: 0;
  display: flex;
  justify-content: flex-end;
  user-select: none;
}

.hover-box {
  width: 28px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.action-close {
  width: 28px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.mize {
  width: 24px;
  height: 24px;
  color: #404040;
  opacity: 0.66;
  cursor: pointer;
}

.rectangle-small {
  width: 18px;
  height: 18px;
  color: #404040;
  cursor: pointer;
}

.internal-reduction {
  width: 16px;
  height: 16px;
  color: #404040;
  cursor: pointer;
}

.close {
  width: 14px;
  height: 14px;
  color: #404040;
  cursor: pointer;
}

.hover-box:hover {
    background-color: rgba(166, 166, 166, 0.2);
}

.action-close:hover {
    background-color: rgb(194 43 28);
}
</style>
